package cn.Alibaba.pet.service.impl;

import cn.Alibaba.basic.exception.BusinessException;
import cn.Alibaba.basic.util.*;
import cn.Alibaba.order.domain.OrderPetAcquisition;
import cn.Alibaba.order.mapper.OrderPetAcquisitionMapper;
import cn.Alibaba.org.domain.Employee;
import cn.Alibaba.org.domain.Shop;
import cn.Alibaba.org.mapper.EmployeeMapper;
import cn.Alibaba.org.mapper.ShopMapper;
import cn.Alibaba.pet.domain.Pet;
import cn.Alibaba.pet.domain.SearchMasterMsg;
import cn.Alibaba.pet.domain.SearchMasterMsgAuditLog;
import cn.Alibaba.pet.dto.AccDto;
import cn.Alibaba.pet.mapper.PetMapper;
import cn.Alibaba.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.Alibaba.pet.mapper.SearchMasterMsgMapper;
import cn.Alibaba.pet.query.SearchMasterMsgQuery;
import cn.Alibaba.pet.service.ISearchMasterMsgService;
import cn.Alibaba.basic.service.impl.BaseServiceImpl;
import cn.Alibaba.user.domain.Logininfo;
import cn.Alibaba.user.domain.User;
import cn.Alibaba.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long id) {

        User user = userMapper.loadByLogininfoId(id);

        searchMasterMsg.setShopId(user.getId());

        // 获取当前地址的点point

        Point point = DistanceUtils.getPoint(searchMasterMsg.getAddress());

        // 获取当前距离最近的地址
        Shop nearestShop = DistanceUtils.getNearestShop(point, shopMapper.loadAll());

        searchMasterMsg.setUserId(nearestShop.getId());

        searchMasterMsgMapper.add(searchMasterMsg);


        // 百度ai审核

        Boolean textBoo = BaiduAuditUtils.TextCensor(searchMasterMsg.getTitle() + searchMasterMsg.getName() + searchMasterMsg.getAddress());
        Boolean imageBoo = BaiduAuditUtils.ImgCensor(searchMasterMsg.getResources());

        if (textBoo){

            //修改状态和日志
            searchMasterMsg.setState(1);
            searchMasterMsg.setNote("待处理！nice");
            searchMasterMsgMapper.update(searchMasterMsg);


            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(searchMasterMsg.getId());
            log.setState(1);
            log.setNote("待处理!");

            searchMasterMsgAuditLogMapper.add(log);
        }else {

            searchMasterMsg.setState(0);
            searchMasterMsg.setNote("审核失败，内容非法");
            searchMasterMsgMapper.update(searchMasterMsg);

            //2.2.审核失败：修改寻主消息状态0 + 添加审核日志
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(searchMasterMsg.getId());
            //audit_id - 自动审核，没有添加审核人
            log.setState(0);//驳回
            log.setNote("审核驳回");
            searchMasterMsgAuditLogMapper.add(log);
            throw new BusinessException("审核失败，内容非法");
        }


    }

    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(1);
        Logininfo logininfo = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if(employee.getShop_id() !=null ){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, HttpServletRequest request) {

        Logininfo loginUser = LoginContext.getLoginUser(request);
        User user = userMapper.loadByLogininfoId(loginUser.getId());

        query.setUserId(user.getId());

        return super.queryPage(query);

    }

    @Override
    public void reject(Long id) {
        searchMasterMsgMapper.reject(id);

    }

    @Override
    public void accept(AccDto dto) {
        Long empId = dto.getHandler();
        Long msgId = dto.getMsgId();
        String note = dto.getNote();

        OrderPetAcquisition orderTem=orderPetAcquisitionMapper.loadByMsgId(msgId);

        if (orderTem!=null){
            throw new BusinessException("订单正在加急处理,请稍后");
        }

        // 创建宠物对象
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadOne(msgId);

        Pet pet = searchMasterMsg2Pet(searchMasterMsg);

        petMapper.add(pet);

        // 创建收购订单
        OrderPetAcquisition order= buildOrder(pet,searchMasterMsg,empId);

        orderPetAcquisitionMapper.add(order);
    }

    private OrderPetAcquisition buildOrder(Pet pet, SearchMasterMsg searchMasterMsg, Long empId) {
        OrderPetAcquisition order = new OrderPetAcquisition();
        String orderSn = CodeGenerateUtils.generateOrderSn(pet.getUserId());
        order.setOrderSn(orderSn);
        order.setDigest(searchMasterMsg.getTitle());
        Long timeTme = System.currentTimeMillis()+2*24*66*60*1000;
        order.setLastcomfirmtime(new Date(timeTme));
        order.setState(0);
        order.setPrice(searchMasterMsg.getPrice());
        order.setShopId(searchMasterMsg.getShopId());
        order.setUserId(searchMasterMsg.getUserId());
        order.setPetId(searchMasterMsg.getId());
        order.setAddress(searchMasterMsg.getAddress());
        order.setEmpId(empId);
        order.setSearchMasterMsgId(searchMasterMsg.getId());

        return order;



    }

    private Pet searchMasterMsg2Pet(SearchMasterMsg searchMasterMsg) {

        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(searchMasterMsg.getPrice().toString());
        pet.setTypeId(searchMasterMsg.getPetType());
        pet.setResources(searchMasterMsg.getResources());
        pet.setShopId(searchMasterMsg.getShopId());
        pet.setUserId(searchMasterMsg.getUserId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        return pet;

    }
}
